<?php
/**
 * Class provides operations through local filesystem
 */
    class classFileSystem{
    /**
     * function returns list of directoryies by the $dirname input parameter.
     * <br><br><b>return value</b> of this function <b>is array of strings</b>!
     */
        public function getDirsList($sScannedDir){
            //create array of directories
            if ($handle = opendir($sScannedDir)) {
                $iIndex = 0;
                while (false !== ($file = readdir($handle))) {
                    if ($file != "." && $file != ".." && is_dir($sScannedDir.$file)) {
                        $dirsList[$iIndex] = $file;
                        $iIndex++;
                    }
                }
                closedir($handle);
                return $dirsList;
            }
            else
            return '';            
        }

    /**
     * function returns list of files by the $dirname input parameter.
     * Input parameter $ext is the array with names of permitted extensions of files
     * <br><br><b>return value</b> of this function <b>is array of strings</b>!
     */
        public function getFilesList($dirname,$ext_array){
            //create array of files
            if ($handle = opendir($dirname)) {
            $iIndex = 0;
                    while (false !== ($file = readdir($handle))) {
                        if ($file != "." && $file != ".." &&
                            !is_dir($dirname.'/'.$file) &&
                            $this->isPermitted($ext_array,$file)!==false
                        ){
                            $filesList[$iIndex] = $file;
                            $iIndex++;
                        }
                    }
                    closedir($handle);
                    return $filesList;
                }
            else
            return '';
        }
    
        public function File_Upload($source_filename,$dirname,$name)
        {
            define("PATH_UPLOAD","$dirname$name");
            $width = 550;
            $hight = 412;
            move_uploaded_file($source_filename,PATH_UPLOAD)
            or die ("Subor sa nepodarilo preniest");
            $size=getImageSize(PATH_UPLOAD);
            if($size[0]>$width or $size[1]>$hight)
            {   $w=0;
                $h=0;
                $image=PATH_UPLOAD;
                $imgRes=PATH_UPLOAD;
                $size=getImageSize($image);
                if($size[0]>$size[1])$w=$width;
                else                 $h=$hight;
                if($h){    $w=$size[0]/($size[1]/$h); }
                else{    $h=$size[1]/($size[0]/$w); }

                $dst_img=imagecreatetruecolor($w,$h);
                $src_img=ImageCreateFromjpeg($image);
                ImageCopyResampled($dst_img,$src_img,0,0,0,0,$w,$h,$size[0],$size[1]);
                ImageJpeg($dst_img,$imgRes,100);
            }

        }
        
        public function File_Delete($dirname,$name) {
            unlink($dirname.$name);
        }
        
        public function Dir_Create($dirname) {
            mkdir($dirname);
        }
        
        public function Dir_Rename($OldDirname,$NewDirname) {
            rename($OldDirname,$NewDirname);
        }
        
        public function Dir_Delete($dirname) {
            $FileList = self::getFilesList($dirname,array('jpg','jpeg','bmp','png','gif','xml'));
            foreach($FileList as $file)
              self::File_Delete($dirname, $file);
            rmdir($dirname);
        }
        
    /**
     * function comapares whether extension of <i>$file</i> input variable is 
     * in content of <i>$ext_array</i> input variable. If not, function returns false
     */
        private function isPermitted($ext_array,$file){
            $file_extension = end(explode(".", $file));
            return array_search($file_extension, (array)$ext_array);
        }
    }

?>
